import { Component, OnInit } from '@angular/core'
import { JsonApiService } from 'app/core/api/json-api.service' //http请求配置引入
import { Router, ActivatedRoute } from '@angular/router' //路由引入
import { config } from '../../shared/smartadmin.config'

declare var $: any
@Component({
  selector: 'sa-device-relieve-record',
  templateUrl: './device-relieve-record.component.html'
})
export class DeviceRelieveRecord implements OnInit {
  tableData: any[] = [] //table数据
  totalCount: number = 0 //分页总条数
  curPage: number = 1 //分页当前页
  pageSize: number = 10 //内置的分页每页条数
  searchKey: string = ''
  startTime: string = ''
  endTime: string = ''
  selectedCompanyId: string = ''
  selectedCompanyName: string = ''
  deviceId: string = ''
  carNumber: string = ''
  limitStatus: string = ''
  constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
    activatedRoute.queryParams.subscribe(queryParams => {
      this.curPage = queryParams.nowPage || 1
    })
  }
  // 接收子组件传递过来的数据
  getSelectTableTime(time) {
    this.startTime = time.startTime
    this.endTime = time.endTime
  }
  ngOnInit() {
    this.load()
  }
  //table刷新
  refresh() {
    this.load()
  }
  //表格搜索
  table_search() {
    this.curPage = 1
    this.load()
  }
  //初始化加载list数据
  load() {
    let timeCheck = common.correctTimeParams(this.startTime, this.endTime, 0)
    if (!timeCheck.status) {
      this.jsonApiService.message(timeCheck.message)
      return
    }
    let data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        factoryId: this.selectedCompanyId,
        limitStatus: this.limitStatus,
        deviceId: this.deviceId,
        startTime: this.startTime,
        endTime: this.endTime,
        searchKey: common.stringTrim(this.searchKey)
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(data)
    this.jsonApiService.http('deviceNormalLimitRecord/pageDeviceNormalLimitRecord.html', reqdata).subscribe(data => {
      if (data.isSuccess) {
        this.tableData = data.exhibitDatas
        this.totalCount = data.recordCount
      } else {
        this.jsonApiService.message(data.message)
      }
    })
  }
  unique(arr) {
    return Array.from(new Set(arr))
  }
  //报表导出
  exportTable() {
    const param = {
      startTime: this.startTime,
      endTime: this.endTime,
      factoryId: this.selectedCompanyId,
      limitStatus: this.limitStatus,
      deviceId: this.deviceId,
      searchKey: this.searchKey.trim()
    }
    let timeCheck = common.correctTime(param)
    if (!timeCheck.status) {
      this.jsonApiService.message(timeCheck.message)
      return
    }
    common.exprotDataFile(param, 'param', config.BaseURL + 'logForDeviceEvent/replacedHostsLogPage.html')
  }
  //分页回调
  paginate(e) {
    this.curPage = e.page + 1
    this.pageSize = e.rows
    this.load()
  }
  //公司选中回调
  companySelected(data) {
    this.selectedCompanyId = data.companyId
    this.selectedCompanyName = data.companyName
    this.deviceId = data.deviceId
    this.carNumber = data.carNumber
  }

  // 解除限制
  releaseRestrictions(row) {
    this.jsonApiService.confirm_message('此操作将解除限制车辆，确认要解除限制?').then(() => {
      let reqdata = 'id=' + row.id
      this.jsonApiService.http('deviceNormalLimitRecord/auditSuccess.html', reqdata).subscribe(data => {
        this.jsonApiService.message(data.message)
        if (data.success) {
          this.load()
        }
      })
    })
  }
  // 删除记录
  deleteRecord(row){
    this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除').then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("deviceNormalLimitRecord/deleteById.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		})
  }
}
